// pages/Mjump/Mjump.js
const app = getApp();
const utils = require('../../utils/util.js');
const post = utils.post;
const jumpWeapp = utils.appid;
const getCurrentPageUrlWithArgs = utils.getCurrentPageUrlWithArgs;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        optionL: '',
        official: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        var that = this;
        var reg = RegExp(/testweapp.whwangdoudou.cn/);
        if (reg.exec(utils.url)) {
            that.setData({
                official: true
            })
        };
        that.setData({
            optionL: options,
            uid: options.uid,
            weappid: options.weappid
        });
        that.fetchData()
    },
    fetchData() {
        let that = this;
        if (that.data.uid && that.data.weappid) {
            let uid = that.data.uid;
            let weappid = that.data.weappid;
            let p = {
                m: 'weapp_info',
                uid: uid,
                weappid: weappid
            };
            post(p).then(res => {
                // console.log(res)
                if (res.r == 0) {
                    that.data.weapp_play_time = parseInt(res.result.play_time);
                    that.data.weapp_jump_play_time = parseInt(res.result.jump_play_time);
                    var _path = res.result.page;
                    if (_path.indexOf('?') != -1) { // 包含问号
                        _path += '&uid=' + that.data.uid + '&weappid=' + that.data.weappid;
                    } else { // 不包含问号
                        _path += '?uid=' + that.data.uid + '&weappid=' + that.data.weappid;
                    }
                    that.setData({
                        appid: res.result.appid,
                        path: _path,
                        picurl: res.result.img_bg,
                    });
                }
            });
        }
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {
        wx.getStorageInfo({
            success: function(res) {
                //console.log(res.keys)
                for (var i = 0; i < res.keys.length; i++) {
                    var key = res.keys[i];
                    //console.log(key);
                    if (0 == key.indexOf('guest_time_start_')) {
                        wx.removeStorageSync(key);
                    } else if (0 == key.indexOf('guest_time_end_')) {
                        wx.removeStorageSync(key);
                    } else if (0 == key.indexOf('weappid')) {
                        wx.removeStorageSync(key);
                    }
                }
            }
        });
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        let that = this;
        app.getLbs();
        if (that.data.optionL.uid) {
            that.data.uid = that.data.optionL.uid;
            wx.setStorageSync('weapp_uid', that.data.uid);
        }
        let weappid = wx.getStorageSync('weappid');
        let guest_time_start = wx.getStorageSync('guest_time_start_' + that.data.uid + '_' + weappid)
        if (that.data.uid && weappid) {
            if (guest_time_start && guest_time_start > 0) {
                let guest_time_end = parseInt(new Date().getTime() / 1000);
                wx.setStorageSync('guest_time_end_' + that.data.uid + '_' + weappid, guest_time_end);
                let guest_time = guest_time_end - guest_time_start;
                let content = {
                    userinfo: wx.getStorageSync('_userinfo'),
                    device_info: wx.getStorageSync('device_info'),
                    guest_time: guest_time
                };
                content = JSON.stringify(content);
                let p = {
                    m: 'invoke',
                    type: 'guest_end',
                    content: content,
                    uid: that.data.uid,
                    weappid: weappid
                };
                post(p).then(res => {
                    wx.removeStorageSync('weappid');
                    wx.removeStorageSync('guest_time_start_' + that.data.uid + '_' + weappid);
                    wx.removeStorageSync('guest_time_end_' + that.data.uid + '_' + weappid);
                    if (guest_time < 30) {

                    } else {

                    }
                })
            }
        }
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    /*onShareAppMessage: function() {

    }*/
})